<?php
require 'db_config.php';
require 'open_db.php';
require_once 'includes/resources.php';
include 'includes/recipes.inc';
include 'includes/ingredients.inc';
include 'includes/units.inc';
include 'includes/modifiers.inc';

include 'includes/header.php';
?>

    <body>
<?php 
  print_r($_GET);
  echo "<br >";
  print_r($_GET['ing_3']);
if(isset($_GET['submit'])){

  
  $r_name = safe($_GET['r_name']);
  $r_content = safe($_GET['r_content']);
  
  
  $last = save_recipe($r_name, $r_content);
   
  
  $num_i_in = safe($_GET['num_i']);
  
  $array_ing = array();

  for ($i=0;$i<$num_i_in;$i++){
    $array_temp = $_GET['ing_'. $i .''];
    
    if(trim($array_temp['q']) == NULL){
     if(empty($array_temp['m'])){$array_temp['m'] = 'NULL';}     
     echo "<p>q =|" . $$array_temp['q'] . "|</p>";
    echo "<p>m =|" . $$array_temp['m'] . "|</p>";
      array_push($array_ing, $array_temp);
    }
  }
 echo "<div>" ;
  print_r($array_ing);
  echo "<div>";
// Create the qyery string
$query = "INSERT INTO `ing_lk` 
                    	( `ing_lk_id` ,
                    	  `r_id` ,
                    	  `ing_q` ,
                    	  `unit_id` ,
                    	  `mod_id` ,
                    	  `ing_id` ) 
                    	 VALUES";
echo $query . "<br/ >";
// Loop through the array
for($i = 0, $c = count($array_ing); $i < $c; $i++) {
	echo $i;
     echo "<p>mod id: " . $array_ing[$i]['m'] . "</p>";
     echo "<p>ing id: " . $array_ing[$i]['i']. "</p>";
    $mod_id = create_mod(safe($array_ing[$i]['m']));
    $ing_id = create_ingredient(safe($array_ing[$i]['i']));
    
// Add the next batch of values to the query string
$query .= "( NULL, {$last} , {$array_ing[$i]['q']}, {$array_ing[$i]['u']}, {$mod_id}, {$ing_id})";
echo $query;
// Add a comma if this is not the last batch
if($i+1 < $c) {
$query .= ", ";
}

}
echo "<p> " . $query . "</p>";
    $result = mysql_query($query);
    if (!$result) {
      die('Invalid query: ' . mysql_error());    
    }

}  



$num_i = 10;
?>
    
<form action="add2.php" method="get">
<table>
<tr>
<td class="form-left"><p>Recipe Name:</p></td><td class="form-right"><p><input name="r_name" size="20" /></p></td>
</tr>
<tr>
<td colspan="2">
<table class="ing-table">
<?php for($i=0;$i<$num_i;$i++){ ?>



<tr>
<td class="form-left"><p>Ingredient:</p></td>
<td><p><input type="text" name="ing_<?php echo $i; ?>[q]" size="4" /></p></td>
<td><p>
<select name="ing_<?php echo $i; ?>[u]">
<?php $units = get_units();
foreach($units as $unit){
  echo "<option ";
  
  echo " value= \"". $unit -> unit_id . "\">" . $unit -> unit_s . "</option>\n";
}
?>
</select>
</p></td>
<td><p><input type="text" class="modifier" name="ing_<?php echo $i; ?>[m]" size="20" /></p></td>
<td><p><input type="text" class="ingredient" name="ing_<?php echo $i; ?>[i]" size="20" /></p></td>
</tr>


<?php }?>
</table>
</td>
</tr>
<tr>
<td class="form-left"><p>Instructions:</p></td><td class="form-right"><div><textarea rows="10" cols="60" id="r_content" name="r_content"></textarea></div></td>
</tr>
<tr>
<td class="form-left"><p><input name="num_i" type="hidden" value="<?php echo $num_i; ?>" /></p></td><td class="form-right"><p><input type="submit" name="submit" value="submit" /></p></td>
</tr>
</table>
</form>
<script type="text/JavaScript">
$("input[class='ingredient']").autoSuggest("http://localhost/meals/ajax/ing.php", {minChars: 2, matchCase: false});
</script>

    </body>

</html>